package net.viralpatel.struts2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.commons.codec.digest.DigestUtils;
import com.opensymphony.xwork2.ActionSupport;

public class RegisterUserAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String userID;
	private String password;
	private String name;
	private String email;
	private String telephone;
	private String company;
	String url = "jdbc:mysql://localhost:3306/";
	String dbName = "t10";
	String driverName = "com.mysql.jdbc.Driver";
	Connection con = null;
	ResultSet resultSet = null;
	String rootname = "root";
	String passwdroot = "";
	public boolean checkUser() throws Exception{		
		try {
			Class.forName(driverName);
			con = DriverManager.getConnection(url + dbName,rootname,passwdroot);
		} catch (ClassNotFoundException e) {
			System.out.println(e.getMessage());	
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		String selectCheck = "SELECT * FROM user WHERE userID= ?";
		PreparedStatement prepStmt = con.prepareStatement(selectCheck);
		prepStmt.setString(1, this.userID);
		resultSet = prepStmt.executeQuery();
		resultSet.last();
		int rowCol = resultSet.getRow();
		if (rowCol == 1) {
			con.close();
			return true;
		} else {
			con.close();
			return false;
		}
	}
	public String execute()throws Exception{

		if(checkUser()==true){
			addActionError(getText("error.addUser"));
			return ERROR;
		}else{
			try {
				Class.forName(driverName);
				con = DriverManager.getConnection(url + dbName,rootname,passwdroot);
			} catch (ClassNotFoundException e) {
				System.out.println(e.getMessage());	
			}catch(Exception e){
				System.out.println(e.getMessage());
			}
			String selectLogin = "Insert into user (userID,password,fullname,mail,tel,company,priority,state) value (?,?,?,?,?,?,?,?)";
			PreparedStatement prepStmt = con.prepareStatement(selectLogin);
			prepStmt.setString(1, this.userID);
			prepStmt.setString(2, DigestUtils.md5Hex(this.password));
			prepStmt.setString(3, this.name);
			prepStmt.setString(4, this.email);
			prepStmt.setString(5, this.telephone);
			prepStmt.setString(6, this.company);
			prepStmt.setInt(7, 0);
			prepStmt.setInt(8, 0);
			int res = prepStmt.executeUpdate();
			if (res == 0) {
				addActionError(getText("error.addUser"));
				con.close();
				return ERROR;
			} else {
				con.close();
				return SUCCESS;
			}
		}
	}
	public void setUserID(String userID){
		this.userID=userID;
	}
	public String getUserID(){
		return this.userID;
	}
	public void setPassword(String password){
		this.password=password;
	}
	public String getPassword(){
		return this.password;
	}
	public void setName(String name){
		this.name=name;
	}
	public String getName(){
		return this.name;
	}
	public void setEmail(String email){
		this.email=email;
	}
	public String getEmail(){
		return this.email;
	}
	public void setTelephone(String telephone){
		this.telephone=telephone;
	}
	public String getTelephone(){
		return this.telephone;
	}
	public void setCompany(String company){
		this.company=company;
	}
	public String getCompany(){
		return this.company;
	}
}
